View Javadoc
1 /* FOREGEJ - FOrmatting REfactoring GEnerating Java 2 * 3 * Copyright (C) 2003 Andreas Arrgard 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 package com.octagroup.foregej.ant; 20 import antlr.RecognitionException; 21 import antlr.TokenStreamException; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.Task; 24 import java.io.File; 25 import java.io.IOException; 26 import java.util.Iterator; 27 import java.util.List; 28 import java.util.StringTokenizer; 29 import com.octagroup.foregej.java.lang.JavaRecognizer; 30 import com.octagroup.foregej.java.lang.ast.AST_EXTENDS_CLAUSE; 31 import com.octagroup.foregej.java.lang.ast.AST_METHOD_DEF; 32 import com.octagroup.foregej.java.lang.ast.AST_PACKAGE_DEF; 33 import com.octagroup.foregej.java.lang.ast.CompilationUnit; 34 import com.octagroup.foregej.java.tools.importcleaner.ImportCleanerTool; 35 /*** 36 * A task that modifies a java file. 37 */ 38 public class JavaModifyTask extends Task 39 { 40 /*** 41 * The source java file 42 */ 43 private File _dst; 44 /*** 45 * the destination java file 46 */ 47 private File _src; 48 /*** 49 * The new superclass 50 */ 51 private String _extends; 52 /*** 53 * The new package of the file 54 */ 55 private String _package; 56 /*** 57 * The methods to remove 58 */ 59 private AST_METHOD_DEF[] _methodsToRemove; 60 /*** 61 * Sets the dst. 62 * 63 * @param dst The dst to set 64 * @throws BuildException DOCUMENT ME! 65 */ 66 public void setDst(String dst) 67 { 68 _dst=new File(getProject().getBaseDir(), dst); 69 if(_dst.exists()&&_dst.canWrite()==false) { 70 throw new BuildException("Cannot write to destination file"); 71 } 72 } 73 /*** 74 * Sets the extends. 75 * 76 * @param _extends_ The extends to set 77 */ 78 public void setExtends(String _extends_) 79 { 80 _extends=_extends_; 81 } 82 /*** 83 * Sets the package. 84 * 85 * @param _package_ The package to set 86 */ 87 public void setPackage(String _package_) 88 { 89 _package=_package_; 90 } 91 /*** 92 * Sets the methods to remove. 93 * 94 * @param methodsToRemove the methods to remove 95 */ 96 public void setMethodsToRemove(String methodsToRemove) 97 { 98 try{ 99 StringTokenizer st=new StringTokenizer(methodsToRemove, ",", 100 false); 101 AST_METHOD_DEF[] methodDefs=new AST_METHOD_DEF[st.countTokens()]; 102 for(int i=0; st.hasMoreTokens(); i++){ 103 String methodSig=st.nextToken(); 104 methodSig+="{}"; 105 JavaRecognizer parser=JavaRecognizer.setup(methodSig); 106 parser.field(); 107 methodDefs[i]=(AST_METHOD_DEF)parser.getAST(); 108 } 109 // 110 // all mehtod signatures parsed ok... 111 // 112 _methodsToRemove=methodDefs; 113 }catch (RecognitionException e) { 114 e.printStackTrace(); 115 throw new BuildException(e.getMessage()); 116 }catch (TokenStreamException e) { 117 e.printStackTrace(); 118 throw new BuildException(e.getMessage()); 119 }catch (RuntimeException e) { 120 e.printStackTrace(); 121 throw new BuildException(e.getMessage()); 122 } 123 } 124 /*** 125 * Sets the src. 126 * 127 * @param src The src to set 128 * @throws BuildException DOCUMENT ME! 129 */ 130 public void setSrc(String src) 131 { 132 _src=new File(getProject().getBaseDir(), src); 133 if(_src.exists()==false) { 134 throw new BuildException("Source file does not exist"); 135 } 136 } 137 /*** 138 * DOCUMENT ME! 139 * 140 * @throws BuildException DOCUMENT ME! 141 */ 142 public void execute() 143 { 144 if(_src==null) { 145 throw new BuildException("Must specify a source file"); 146 } 147 if(_dst==null) { 148 throw new BuildException("Must specify a destination file"); 149 } 150 try{ 151 System.out.println("Parsing file:"+_src); 152 CompilationUnit cu=new CompilationUnit(_src); 153 cu.read(); 154 if(_package!=null) { 155 cu.setAstPackage(new AST_PACKAGE_DEF(_package)); 156 } 157 if(_extends!=null) { 158 cu.getAstClassDef().setAstExtendsClause(new AST_EXTENDS_CLAUSE(_extends)); 159 } 160 if(_methodsToRemove!=null) { 161 removeMethods(_methodsToRemove, cu); 162 } 163 ImportCleanerTool.getInstance().process(cu); 164 System.out.println("Writing file:"+_src); 165 cu.setSource(_dst); 166 cu.write(); 167 }catch (TokenStreamException e) { 168 e.printStackTrace(); 169 throw new BuildException("Modification of java file failed:"+e.getMessage()); 170 }catch (RecognitionException e) { 171 e.printStackTrace(); 172 throw new BuildException("Modification of java file failed:"+e.getMessage()); 173 }catch (IOException e) { 174 e.printStackTrace(); 175 throw new BuildException("Modification of java file failed:"+e.getMessage()); 176 }catch (RuntimeException e) { 177 e.printStackTrace(); 178 throw new BuildException("Modification of java file failed:"+e.getMessage()); 179 } 180 } 181 /*** 182 * Removes the suplied methods from the supplied compilation unit. 183 * 184 * @param methodsToRemove the methods to remove 185 * @param cu the compilation unit to remove the methods from 186 */ 187 private void removeMethods(AST_METHOD_DEF[] methodsToRemove, 188 CompilationUnit cu) 189 { 190 List methods=cu.getAstClassDef().getAstBody().getAstMethods(); 191 Iterator iter=methods.iterator(); 192 while(iter.hasNext()){ 193 AST_METHOD_DEF method=(AST_METHOD_DEF)iter.next(); 194 for(int i=0; i<methodsToRemove.length; i++){ 195 if(method.hasSameSignature(methodsToRemove[i])) { 196 System.out.println("Found method to remove..."); 197 iter.remove(); 198 } 199 } 200 } 201 } 202 }

This page was automatically generated by Maven